<template>
  <el-container>
    <el-header height="100px">
      <el-row :gutter="20">
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <img src="../assets/logo.png" alt="" width="70px" class="logoimg" />
          </div>
        </el-col>
        <el-col :span="16">
          <div class="grid-content bg-purple fontSize" @click="adds">
            用户详情页面
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-dropdown @command="goOut">
              <span class="el-dropdown-link">
                {{ userinfo.username }}
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="a">退出</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-col>
      </el-row>
    </el-header>
    <el-container class="content">
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          :unique-opened="true"
          :router="true"
        >
          <el-submenu :index="v.path" v-for="(v, k) in menusData" :key="k">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{ v.authName }}</span>
            </template>
            <el-menu-item-group>
              <el-menu-item
                v-for="(v2, k2) in v.children"
                :key="k2"
                :index="v2.path"
                >{{ v2.authName }}</el-menu-item
              >
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      userinfo: {},
      menusData: [],
      router: true,
    };
  },
  methods: {
    adds() {
      this.$store.commit("increment");
      console.log(this.$store.state.count);
    },
    goOut() {
      localStorage.removeItem("userinfo");
      this.$router.push({ path: "/login" });
    },
  },
  //这是挂载之后的钩子函数，这时候页面已经渲染完成了
  async mounted() {
    //将本地储存的内容储存给data中的userinfo
    this.userinfo = JSON.parse(localStorage.getItem("userinfo"));

    //获取侧边信息，进行遍历
    const { data } = await this.$req.get("/menus");
    if (data.meta.status == 200) {
      this.menusData = data.data;
    }
  },
  //这是在页面加载之前就进行的钩子函数
  /* beforeCreate() {
    //判断用户身份不能在组件中
    //需要在跳转之后，并且在组件的加载之前，进行用户身份的验证
    //这就需要路由中的 导航守卫
    var localstorage = localStorage.getItem("userinfo");
    if(!localstorage){
      this.$router.push({path:'/'});
    }
  }, */
  //在页面一加载就进行判断cookie是否存在
};
</script>

<style>
.content {
  height: 800px;
}
.el-header {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.logoimg {
  margin-top: 20px;
}
.fontSize {
  font-size: 30px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}
</style>